<?
if($_SESSION['sid'] != 1){
    exit('are you ok?');
}
ob_start();
include("common/header.html");
include("common/left.html");
include("common/page.php");
include("class/SimpleExcel.class.php");
$out_html=ob_get_contents();
ob_end_clean();

$r=array();
$pageSize=$_REQUEST['pageSize']?$_REQUEST["pageSize"]:10;
$currentPage=$_REQUEST["currentPage"]?$_REQUEST["currentPage"]:1;
$offset=($currentPage-1)*$pageSize;
if(isset($_REQUEST['act']) && $_REQUEST['act']=="Export")
{
	$currentPage = 1;
	$pageSize = 9999999;
}
$where1="where 1=1";
$dname=$_REQUEST['dname'];
//根据输入的分销商信息查询条件
if($dname&&is_numeric($dname)&&strlen($dname)==6||$dname=='admin'){
    $str="select id from pft_member where account='$dname'";
    $GLOBALS['le']->query($str);
    while($row=$GLOBALS['le']->fetch_assoc()){
        $ids[]=$row['id'];
    }
    if(count($ids)!=0){
		$ids=implode(",",$ids);
		$where1.=" and id in ($ids)";
	}else{
		$where1.=" and 1<>1";
	}
}elseif($dname=mysql_real_escape_string(trim($_REQUEST['dname']))){
	$ids=array();
	$str="select id from pft_member where dname like '%".$dname."%'";
	$GLOBALS['le']->query($str);
	while($row=$GLOBALS['le']->fetch_assoc()){
		$ids[]=$row['id'];
	}
	if(count($ids)!=0){
		$ids=implode(",",$ids);
		$where1.=" and id in ($ids)";
	}else{
		$where1.=" and 1<>1";
	}
}

$where2="where 1=1";
//if($beginDate=mysql_real_escape_string($_REQUEST['beginDate'])){
//	$where2.=" and rectime>='$beginDate 00:00:00'";
//}
//if($endDate=mysql_real_escape_string($_REQUEST['endDate'])){
//	$where2.=" and rectime<='$endDate 23:59:59'";
//}

//只取输入的日期之前最后一笔收支
//原来的算法是取限定时间之内的 这样有的商家那天没有交易就取不到他的余额了
//2015-06-18 zax
if($beginDate=mysql_real_escape_string($_REQUEST['beginDate'])){
	$where2.=" and rectime<='$beginDate 23:59:59'";
}
$where2.=" and ptype in (0,1)";

$dnames=array();
$str="select id,dname,account from pft_member $where1 and dtype!=2 and dtype!=5 and dtype!=6 and status=0 and group_id!=2 limit $offset,$pageSize";
$GLOBALS['le']->query($str);
$dn=array();
while($row=$GLOBALS['le']->fetch_assoc()){
    $dn[$row['id'].'_'.$row['account']]=$row['dname'];
	$dnames[$row['id']]=$row['dname'];
}
//将商户的account，id，dname存入ainfo数组
$ainfo=$t=array();
foreach($dn as $key=>$d){
    $k=explode('_',$key);
    $t['id']=$k[0];
    $t['dname']=$d;
    $t['account']=$k[1];
    $ainfo[]=$t;
    unset($t);
}
foreach($ainfo as $a=>$i){
    if(is_numeric($dname)&&strlen($dname)==11) continue;
    $str="select id,fid,dmoney,lmoney,aid,rectime,dtype,opid,ptype,'' as ali_account from pft_member_journal $where2 and fid={$i['id']} order by id desc,rectime desc limit 1";

    // $str="(select id,fid,dmoney,lmoney,aid,orderid,daction,rectime,dtype,ptype,'' as ali_account from pft_member_journal $where1) UNION (select id,fid,money,'','',orderid,0,rectime,-1,-1,ali_account from pft_member_alipay  $where2) order by rectime desc,orderid desc limit $offset,$pageSize";
    if($beginDate){
        $GLOBALS['le']->query($str);
        $row=$GLOBALS['le']->fetch_assoc();
        if($row){
            $row['account']=$i['account'];
            $row['member']=$i['dname'];
            $r[]=$row;
        }else{
            $r1['account']=$i['account'];
            $r1['member']=$i['dname'];
            $r1['fid']=$i['id'];
            $r[]=$r1;
        }
    }
}
//print_r($r);
//exit;
if($_REQUEST['cs']){
    echo $str;print_r($r);exit;
}
if(isset($_REQUEST['act']) && $_REQUEST['act']=="Export"){
	$filename=date('YmdHis').'账户余额';
	$Ecel[0]['account']='商户ID';
	$Ecel[0]['member']='商户名称';
	$Ecel[0]['lmoney']='账户余额';
    $Ecel[0]['rectime']='记录时间';
	$i=1;
    
	foreach($r as $lid=>$row){
		if($row['lmoney']==0) continue;
		$n++;
        $Ecel[$n]['account']=$row['account'];
        $Ecel[$n]['member']=$row['member'];
		$Ecel[$n]['lmoney']=$row['lmoney']/100;
        $Ecel[$n]['rectime']=$row['rectime'];
	}
	$xs = new SimpleExcel('UTF-8', true,'accountSearchList');
    $xs->addArray($Ecel);
    $xs->generateXML($filename);
	exit;
}

$str="select id,account from pft_member $where1 and dtype!=2 and dtype!=5 and dtype!=6 and status=0";
$GLOBALS['le']->query($str);
while($row=$GLOBALS['le']->fetch_assoc()){
	$rr[]=$row;
}
$arr=array();
$count = $total_money = 0;
foreach($rr as $rw){
	if(is_numeric($rw['account'])&&strlen($rw['account'])==11) continue;
    ++$count;
    if($rw['id'] == '1') continue;//不统计管理员的余额
    $sql = "select lmoney from pft_member_journal $where2 and fid={$rw['id']} order by id desc,rectime desc limit 1";
    if($beginDate){
        $GLOBALS['le']->query($sql);
        while($row=$GLOBALS['le']->fetch_assoc()){
            $total_money += $row['lmoney'];
        }
    }
} 

$currentPage=$_REQUEST['currentPage']?abs($_REQUEST['currentPage']):1;
$pageSize=$_REQUEST['pageSize']?abs($_REQUEST['pageSize']):10;
$offset=($currentPage-1)*$pageSize;
echo $out_html;
include("tpl/accountSearch.html");
include("common/footer.html");
?>


